package com.hearwego;

import java.io.IOException;
import java.util.List;

import org.w3c.dom.ls.LSInput;

import com.hearwego.DetalleFragment.getDetalle;
import com.hearwego.utilities.Informacion;
import com.hearwego.utilities.RssParserPull;

import android.app.Activity;
import android.app.ProgressDialog;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class LocationListFragment extends ListFragment {
	
	static List<Informacion> informacion;
	public LocationModel[] getAudioList;
	static ProgressDialog dialog;
	static Activity activity;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        
        activity = getActivity();
        new getAudioList().execute("");

    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Activity activity = getActivity();
        if (activity != null) {   
            ListAdapter listAdapter = getListAdapter();
            LocationModel locationModel = (LocationModel) listAdapter.getItem(position);
            
            String url = locationModel.archivo; // your URL here
//            MediaPlayer mediaPlayer = new MediaPlayer();
//            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//            try {
//				mediaPlayer.setDataSource(url);
//			} catch (IllegalArgumentException e) {
//				e.printStackTrace();
//			} catch (IllegalStateException e) {
//				e.printStackTrace();
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//            try {
//				mediaPlayer.prepareAsync();
//			} catch (IllegalStateException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//            mediaPlayer.start();
            
            
            
//            http://www.smartthinking.com.mx:8080/HearWeGo/contenido/115mercadoartesaniascoyo.mp3
            
//            Toast.makeText(activity, "presionaste" + locationModel.address, Toast.LENGTH_SHORT).show();
        }
    }
    
    
    public class getAudioList extends AsyncTask<String, String, String>{

    	@Override
    	protected void onPreExecute() {
    		super.onPreExecute();
			dialog = ProgressDialog.show(activity, "","Cargando. Por favor espere...", true);

    	}
    	
		@Override
		protected String doInBackground(String... arg0) {
			RssParserPull parse = new RssParserPull("http://www.smartthinking.com.mx:8080/HearWeGo/DataServlet?Query=select%20*%20from%20audios%20where%20poi_idr%20=%205&BaseDatos=hwg");
			informacion = null;
			informacion = parse.parse();
			return null;
		}
		
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			
			Log.i("informatioAudio", ""+informacion.size());
			for(int i =0; i < informacion.size(); i++){
			 LocationModel[] sLocations = { 
			        new LocationModel(informacion.get(i).getId(), informacion.get(i).getTitulo(), informacion.get(i).getSubtitulo(), informacion.get(i).getAudio())
			    };
			 
			 getAudioList = sLocations;
			 Log.i("Audios", ""+getAudioList.length);
		        if (activity != null) {
		            ListAdapter listAdapter = new LocationModelListAdapter(activity, getAudioList);
		            setListAdapter(listAdapter);
		        }
			}
			dialog.dismiss();
		}
    	
    }
}
